﻿<%@ Page Title="" Language="C#" MasterPageFile="~/forumMaster.Master" AutoEventWireup="true" CodeBehind="Thread.aspx.cs" Inherits="forum.Thread" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">

      <script type="text/javascript">
      var postToUpdate;
      $(document).ready(function () {


          $(".deletePost").live("click", function () {

              if (confirm("Delete post?")) {

                  var parameters;
                  var postId = $(this).attr("postId");
                  parameters = "{'postId':'" + postId + "'}";
                  $.ajax({
                      type: "POST",
                      url: "../Ajax/forumAjax.asmx/deletepost",
                      contentType: "application/json; charset=utf-8",
                      data: parameters,
                      success: function (result) {
                          if (result.d != "bad") {
                              location.reload();
                              return false;
                          }
                      },
                      error: function () {
                          return false;
                      }
                  });
              }
              return false;
          });



          $("#lnkbtnAddPost").click(function () {
              $("#txtaddPostTitle").val("");
              $("#txtAddPostBody").val("");
              open($("#addPostPopUp"));
              return false;
          });


          $("#addPostPopUp").dialog({
              autoOpen: false,
              title:"Add post",
              modal: true,
              buttons: {
                  "ok": function () {

                      var threadId = $("#hdnfldThreadId").val();
                      var title = $("#txtaddPostTitle").val();
                      var body = $("#txtAddPostBody").val();
                      if (title != "" && body != "") {
                          var parameters = "{'threadId':'" + threadId + "','title': '" + title + "','body': '" + body + "'}";
                          $.ajax({
                              type: "POST",
                              url: "../Ajax/forumAjax.asmx/addPost",
                              contentType: "application/json; charset=utf-8",
                              data: parameters,
                              dataType: "json",
                              success: function (result) {

                                  if (result.d != "bad") {

                                      setTimeout(function () { location.reload(); }, 1000);
                                      return false;
                                  }
                                  else {
                                      alert("went wrong!");
                                      return false;
                                  }
                              },
                              error: function () {
                              }
                          });



                      }
                      else {
                          alert("Must input Title or Body");
                          return false;
                      }




                      $(this).dialog("close");
                      return false;
                  },
                  "cancel": function () {
                      $(this).dialog("close");
                  }
              }
          });




          $(".editPost").click(function () {
              $("#txteditPostTitle").val("");
              $("#txteditPostBody").val("");
              var postId = $(this).attr("postId");
              postToUpdate = postId;
              var parameters = "{'postId':'" + postId + "'}";
              $.ajax({
                  type: "POST",
                  url: "../Ajax/forumAjax.asmx/getPostDetails",
                  contentType: "application/json; charset=utf-8",
                  data: parameters,
                  dataType: "json",
                  success: function (result) {

                      if (result.d != "bad") {
                          var array = result.d.split(',');
                          var ajaxTitle = array[0];
                          var ajaxBody = array[1];
                          $("#txteditPostTitle").val(ajaxTitle);
                          $("#txteditPostBody").val(ajaxBody);

                          return false;
                      }
                      else {
                          alert("user name and password are wrong!");
                          return false;
                      }
                  },
                  error: function () {
                  }
              });



              open($("#editPostPopUp"));
              return false;
          });


          $("#editPostPopUp").dialog({
              autoOpen: false,
              title: "Edit post",
              modal: true,
              buttons: {
                  "ok": function () {
                      var postId = postToUpdate;
                      var title = $("#txteditPostTitle").val();
                      var body = $("#txteditPostBody").val();

                      if (title != "" || body != "") {
                          var parameters = "{'postId':'" + postId + "','title': '" + title + "','body': '" + body + "'}";
                          $.ajax({
                              type: "POST",
                              url: "../Ajax/forumAjax.asmx/editPost",
                              contentType: "application/json; charset=utf-8",
                              data: parameters,
                              dataType: "json",
                              success: function (result) {

                                  if (result.d != "bad") {

                                      setTimeout(function () { location.reload(); }, 1000);
                                      return false;
                                  }
                                  else {
                                      alert("user name and password are wrong!");
                                      return false;
                                  }
                              },
                              error: function () {
                              }
                          });



                      }
                      else {
                          alert("Must input Title or Body");
                          return false;
                      }




                      $(this).dialog("close");
                      return false;
                  },
                  "cancel": function () {
                      $(this).dialog("close");
                  }
              }
          });


      });
      </script>
        


       <style type="text/css">
       

       .editPost
       {
           color:Blue;
       }
       
       .editPost:hover
       {
           text-decoration: underline;
           cursor:pointer;
       }
       
       .deletePost
       {
            color:Blue;
       }
       
        .deletePost:hover
       {
           text-decoration: underline;
           cursor:pointer;
       }
       
       
       .post
       {
           margin-top:15px;
       }
       
       #divAddPost
       {
           margin-top:10px;
           margin-bottom:20px;
       }
       
       #txtAddPostBody
       {
           width: 255px; 
           height: 172px;
       }
       
       #txtaddPostTitle
       {
         display: block;
       }
       
       
       #txteditPostBody
       {
           width: 255px; 
           height: 172px;
       }
       
       #txteditPostTitle
       {
         display: block;
       }
       

       #threaTitle
       {
           margin-top:10px;
       }


        #spanThreadTitle
       {
           color:Red;
       }
       #hdnfldThreadId
       {
           display:none;
       }
       
       #lblSubForumeName
       {
          color:Red; 
       }
       
       

       
    </style>

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <div id="divThreadTitle">
           <h3 id="headThread">View thread posts</h3>
    </div>
    <div id="divSubForumName">
        <asp:Label ID="lblSubForumeName"  ClientIDMode="Static" runat="server" Text=""></asp:Label>
    </div>
    <div id="threaTitle">
        <span id="spanThreadTitle">Thread is:</span>
    </div>
    <div id="threadTitle">
             <div class="top">
                   <asp:Label ID="lblUserName" ClientIDMode="Static" runat="server" Text=""></asp:Label> 
                   <asp:Label ID="lblCreationDate" ClientIDMode="Static"  runat="server" Text=""></asp:Label>  
              </div>
              <div class="content">
                    <asp:Label ID="lblThreadTitile" ClientIDMode="Static"  runat="server" Text=""></asp:Label> 
                    <asp:Label ID="lblContent" ClientIDMode="Static"  runat="server" Text=""></asp:Label> 
              </div>
    </div>

    <div id="divAddPost">
       <asp:LinkButton ID="lnkbtnAddPost" ClientIDMode="Static" runat="server">Add Post</asp:LinkButton>
   </div>

    <div id="divThreadPostsTitle">
        <asp:Label ID="lblThreadPostsTitle" runat="server" Text="Threads posts:"></asp:Label>
    </div>

    <div class="divThreadPosts">
            <asp:ListView ID="lvPosts" runat="server" OnItemDataBound="lvPosts_ItemDataBound">
                <EmptyDataTemplate>
                    <span>Thread post list is empty</span>
                </EmptyDataTemplate>
                <ItemTemplate>
                    <li>
                        <div class="post">
                            <div class="top">
                                <asp:Label ID="lblUserName" class="userNameClass" runat="server" Text=""></asp:Label> 
                                <asp:Label ID="lblCreationDate" class="creationDateClass" runat="server" Text=""><%# Eval("creationDate")%></asp:Label>  
                            </div>
                            <div class="content">
                               <a class="postLink" href="Post.aspx?PostId=<%# Eval("postId") %>" >  <%# Eval("postTitle")%> </a>
                               <asp:Label ID="lblContent" class="contentClass" runat="server" Text=""><%# Eval("postContent")%></asp:Label> 
                            </div>
                            <asp:HyperLink ID="hyplnkEdit" postId='<%# Eval("postId")%>' class="editPost" runat="server">edit</asp:HyperLink>
                            <asp:HyperLink ID="lbkbtnDelete" class="deletePost" postId='<%# Eval("postId")%>' runat="server">delete</asp:HyperLink>
                        </div>
                    </li>
                </ItemTemplate>
                <LayoutTemplate>
                    <ul>
                        <asp:PlaceHolder ID="itemPlaceholder" runat="server" />
                    </ul>
                </LayoutTemplate>
            </asp:ListView>
   </div>

    <div id="addPostPopUp">
            <asp:Label ID="lbladdPostTitle" class="popUpTitle" ClientIDMode="Static" runat="server" Text="Title"></asp:Label>
            <asp:TextBox ID="txtaddPostTitle" ClientIDMode="Static" runat="server" ></asp:TextBox>
            <asp:Label ID="lbladdPostBody" class="popUpTitle" ClientIDMode="Static" runat="server" Text="Body"></asp:Label>
            <textarea id="txtAddPostBody" rows="5" cols="5"></textarea>
   </div>

    <div id="editPostPopUp">
            <asp:Label ID="lbleditPostTitle" class="popUpTitle" ClientIDMode="Static" runat="server" Text="Title"></asp:Label>
            <asp:TextBox ID="txteditPostTitle" ClientIDMode="Static" runat="server" ></asp:TextBox>
            <asp:Label ID="lbleditPostBody" class="popUpTitle" ClientIDMode="Static" runat="server" Text="Body"></asp:Label>
            <textarea id="txteditPostBody" rows="5" cols="5"></textarea>
   </div>

     <asp:HiddenField ID="hdnfldThreadId"  ClientIDMode="Static" runat="server" />
</asp:Content>
